Conditions | 4 |
Paths | 8 |
Total Lines | 138 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
20 | function Slf4j (name, context) { |
||
21 | if (!context) { |
||
22 | context = DefaultContext |
||
23 | } |
||
24 | // backward compatibility for 0.1.x/0.2.x |
||
25 | if (!(context instanceof Context)) { |
||
26 | context = new Slf4j.Context(context, arguments[2]) |
||
27 | } |
||
28 | var mdc = {} |
||
29 | |||
30 | function substitute (pattern, substitutions) { |
||
31 | var e = substitutions.filter(function (s) { return s instanceof Error }) |
||
32 | var o = substitutions.filter(function (s) { return !(s instanceof Error) }) |
||
33 | var s = o.reduce(function (carrier, s) { |
||
34 | return carrier.replace('{}', render(s)) |
||
35 | }, pattern) |
||
36 | |||
37 | return e.reduce(function (carrier, e) { |
||
38 | return carrier + '\n' + renderException(e) |
||
39 | }, s) |
||
40 | } |
||
41 | |||
42 | function renderException (e) { |
||
43 | return e.name + ': ' + e.message + '\nStack:\n' + e.stack |
||
44 | } |
||
45 | |||
46 | function render (v) { |
||
47 | v = typeof v === 'undefined' ? '{undefined}' : v |
||
48 | // noinspection EqualityComparisonWithCoercionJS |
||
49 | return v != null && v.constructor === String ? v : JSON.stringify(v) |
||
50 | } |
||
51 | |||
52 | function log (level, pattern) { |
||
53 | level = levelOf(level) |
||
54 | if (toThreshold(level) < toThreshold(context.getLevel(name))) { |
||
55 | return |
||
56 | } |
||
57 | var prefix = '[' + level.toUpperCase() + ']' |
||
58 | if (name) { |
||
59 | prefix += ' ' + name |
||
60 | } |
||
61 | var mdcPrefix = Object.keys(mdc).map(function (key) { |
||
62 | return key + '=' + render(mdc[key]) |
||
63 | }).join(', ') |
||
64 | if (mdcPrefix) { |
||
65 | prefix += ' [' + mdcPrefix + ']' |
||
66 | } |
||
67 | var subs = [].slice.call(arguments, 2) |
||
68 | context.getWriter(name).write(substitute(prefix + ': ' + pattern, subs)) |
||
69 | } |
||
70 | |||
71 | // noinspection JSUnusedGlobalSymbols |
||
72 | this.log = log |
||
73 | |||
74 | for (var i = Threshold.TRACE; i <= Threshold.ERROR; i++) { |
||
75 | this[levelOf(i).toLowerCase()] = (function (self, threshold) { |
||
76 | return function () { |
||
77 | log.apply(null, ([threshold].concat([].slice.call(arguments)))) |
||
78 | } |
||
79 | })(this, i) |
||
80 | } |
||
81 | |||
82 | this.getName = function () { return name } |
||
83 | |||
84 | this.setWriter = function (writer) { |
||
85 | context.setWriter(name, writer) |
||
86 | return this |
||
87 | } |
||
88 | |||
89 | this.getWriter = function () { return context.getWriter(name) } |
||
90 | |||
91 | this.setLevel = function (level) { |
||
92 | context.setLevel(name, level) |
||
93 | return this |
||
94 | } |
||
95 | |||
96 | this.getLevel = function () { return context.getLevel(name) } |
||
97 | |||
98 | /** |
||
99 | * @deprecated |
||
100 | */ |
||
101 | this.setThreshold = this.setLevel |
||
102 | |||
103 | /** |
||
104 | * @deprecated |
||
105 | */ |
||
106 | this.getThreshold = this.getLevel |
||
107 | |||
108 | this.getContext = function () { return context } |
||
109 | |||
110 | this.attach = function (name, value) { mdc[name] = value } |
||
111 | |||
112 | this.detach = function (name) { delete mdc[name] } |
||
113 | |||
114 | this.attachAll = function (values) { mdc = values } |
||
115 | |||
116 | this.detachAll = function () { |
||
117 | var b = mdc |
||
118 | mdc = {} |
||
119 | return b |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @function Slf4j#trace |
||
124 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
125 | * @param {...object} parameters List of pattern parameters |
||
126 | */ |
||
127 | |||
128 | /** |
||
129 | * @function Slf4j#debug |
||
130 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
131 | * @param {...object} parameters List of pattern parameters |
||
132 | */ |
||
133 | |||
134 | /** |
||
135 | * @function Slf4j#notice |
||
136 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
137 | * @param {...object} parameters List of pattern parameters |
||
138 | */ |
||
139 | |||
140 | /** |
||
141 | * @function Slf4j#info |
||
142 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
143 | * @param {...object} parameters List of pattern parameters |
||
144 | */ |
||
145 | |||
146 | /** |
||
147 | * @function Slf4j#warn |
||
148 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
149 | * @param {...object} parameters List of pattern parameters |
||
150 | */ |
||
151 | |||
152 | /** |
||
153 | * @function Slf4j#error |
||
154 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
155 | * @param {...object} parameters List of pattern parameters |
||
156 | */ |
||
157 | } |
||
158 | |||
267 |